package com.lemon.business.timedtask.job;

import com.lemon.business.dao.ConfigDao;
import com.lemon.utils.JedisUtil;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.JedisCluster;

import java.util.Map;

/**
 * @Author liuli
 * @Date 2020/10/13 20:55
 */
public class CacheConfigJob implements Job {
    private static final Logger LOG = LoggerFactory.getLogger(CacheConfigJob.class);

    private static long lastUpdateTime = 0L;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        // 缓存数据库配置到redis
        Map<String, Map<String, String>> machineMap = ConfigDao.selectMachine(lastUpdateTime);
        if (machineMap.size() > 0) {
            lastUpdateTime = System.currentTimeMillis();
        }
        JedisCluster jedis = JedisUtil.getJedisCluster();
        for (Map.Entry<String, Map<String, String>> entry : machineMap.entrySet()) {
            jedis.hset(entry.getKey(), entry.getValue());
        }
        LOG.info("CacheConfig success.");
    }
}
